package com.huqing.icu.controller;

import com.huqing.icu.base.BaseController;
import com.huqing.icu.base.IdReq;
import com.huqing.icu.req.app.chatsession.ChatSessionCreateReq;
import com.huqing.icu.req.app.chatsession.ChatSessionHistoryMessageReq;
import com.huqing.icu.req.app.chatsession.ChatSessionUpdateTitleReq;
import com.huqing.icu.service.IChatSessionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Description 对话记录接口
 * @Author huqing
 * @Date 2025/7/26 17:47
 **/
@RestController
@RequestMapping("/api/chat_session")
@Tag(name = "AI对话记录接口")
public class ChatSessionController extends BaseController {

    @Autowired
    private IChatSessionService iChatSessionService;

    @Operation(summary = "创建新对话")
    @PostMapping(value = "/create")
    public String historyMessage(@RequestBody @Valid ChatSessionCreateReq createReq, HttpServletRequest request) {
        Long userId = checkAndGetUserId(request);
        return jsonSuccessForNull(iChatSessionService.create(userId, createReq.getTitle()));
    }

    @Operation(summary = "修改标题")
    @PostMapping(value = "/update_title")
    public String updateTitle(@RequestBody ChatSessionUpdateTitleReq updateTitleReq, HttpServletRequest request) {
        Long userId = checkAndGetUserId(request);
        return jsonSuccessForNull(iChatSessionService.updateTitle(updateTitleReq.getId(), userId, updateTitleReq.getTitle()));
    }

    @Operation(summary = "删除对话")
    @PostMapping(value = "/delete")
    public String historyMessage(@RequestBody IdReq IdReq, HttpServletRequest request) {
        Long userId = checkAndGetUserId(request);
        return jsonSuccessForNull(iChatSessionService.delete(IdReq.getId(), userId));
    }

    @Operation(summary = "查询对话历史")
    @PostMapping(value = "/history_message")
    public String historyMessage(@RequestBody ChatSessionHistoryMessageReq historyMessageReq, HttpServletRequest request) {
        Long userId = checkAndGetUserId(request);
        historyMessageReq.setUserId(userId);
        return jsonSuccessForNull(iChatSessionService.historyMessage(historyMessageReq));
    }

    @Operation(summary = "导出对话")
    @GetMapping(value = "/export_message")
    public void exportMessage(@RequestParam("chatSessionId") Long chatSessionId, HttpServletRequest request, HttpServletResponse response) {
        //Long userId = checkAndGetUserId(request);
        iChatSessionService.exportMessage(null, null, response);
    }
}
